﻿//************************************************************************//
//文件功能：字符串操作类
//创 建 人：薄帅
//创建时间：2011年05月26日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ProjBackEnd.Common.Helper
{
    /// <summary>
    /// 字符串操作类
    /// </summary>
    public sealed class OperateString
    {
        /// <summary>
        /// 截取字符串左面部分的指定长度
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="intLen">指定长度</param>
        /// <returns>字符串</returns>
        public static string Left(string str, int intLen)
        {
            string strReturn;
            if (str == null || intLen <= 0)
                return "";
            str = str.PadRight(intLen, Convert.ToChar(" "));
            strReturn = str.Substring(0, intLen).Trim();
            return strReturn;
        }

        /// <summary>
        /// 截取字符串右面部分的指定长度
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="intLen">指定长度</param>
        /// <returns>字符串</returns>
        public static string Right(string str, int intLen)
        {
            string strReturn;
            if (str == null || intLen <= 0)
                return "";
            str = str.PadLeft(intLen, Convert.ToChar(" "));
            strReturn = str.Substring(str.Length - intLen, intLen).Trim();
            return strReturn;
        }

        /// <summary>
        /// 获取指定数量的空格
        /// </summary>
        /// <param name="intLen">空格的数量</param>
        /// <returns>字符串</returns>
        public static string Space(int intLen)
        {
            return "".PadRight(intLen, ' ');
        }

        /// <summary>
        /// 字符串右补空格
        /// </summary>
        /// <param name="str"></param>
        /// <param name="intLen"></param>
        /// <returns></returns>
        public static string Space(string str, int intLen)
        {
            return str.Trim().PadRight(intLen, ' ');
        }

        /// <summary>
        /// 字符串左补空格
        /// </summary>
        /// <param name="str"></param>
        /// <param name="intLen"></param>
        /// <returns></returns>
        public static string SpaceLeft(string str, int intLen)
        {
            return str.Trim().PadLeft(intLen, ' ');
        }

        /// <summary>
        /// 将字符串按长度在左侧补'0'
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string FormatNum(string str, int length)
        {
            return str.Trim().PadLeft(length, '0');
        }

        /// <summary>
        /// 将字符串按长度拆分成数组
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static ArrayList SplitToArray(string str, int len)
        {
            ArrayList arr = new ArrayList();

            int count = str.Length / len;
            if (count <= 0)
                arr.Add(str);
            else
            {
                while (true)
                {
                    if (str.Length <= len)
                    {
                        arr.Add(str.Substring(0));
                        break;
                    }

                    arr.Add(str.Substring(0, len));
                    str = str.Substring(len);
                }
            }
            return arr;
        }

        /// <summary>
        /// 按起始位置和长度取出带有汉字的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="startIndex"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string MutiSubString(string str, int startIndex, int length)
        {
            int i = 0;
            for (; i < startIndex; i++)
            {
                if (Convert.ToInt32(str[i]) > 225)
                    startIndex--;
            }

            i = startIndex;
            int l = length;
            for (; i < startIndex + length; i++)
            {
                if (Convert.ToInt32(str[i]) > 225)
                    l--;
            }

            return str.Substring(startIndex, l);
        }

        /// <summary>
        /// 字符串反向输出
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ReserveString(string s)
        {
            char[] c = s.ToCharArray();
            Array.Reverse(c);
            return new string(c);
        }
        /// <summary>
        /// 返回字符的ASC码
        /// </summary>
        /// <param name="c">字符</param>
        /// <returns></returns>
        public static int Asc(char c)
        {
            return Microsoft.VisualBasic.Strings.Asc(c);
        }
        /// <summary>
        /// 返回ASC码的字符
        /// </summary>
        /// <param name="i">ASC码</param>
        /// <returns></returns>
        public static char Chr(int i)
        {
            return Microsoft.VisualBasic.Strings.Chr(i);
        }

        /// <summary> 
        /// 将 Byte[] 转换为 String 
        /// </summary> 
        /// <param name="Data"></param> 
        /// <returns></returns> 
        public static string ByteToString(Byte[] Data)
        {
            return Encoding.Default.GetString(Data);
        }

        /// <summary> 
        /// 将 String 转换为 Byte[] 
        /// </summary> 
        /// <param name="Date"></param> 
        /// <returns></returns> 
        public static Byte[] StringToByte(string Date)
        {
            return Encoding.Default.GetBytes(Date);
        }

        /// <summary>
        /// 生成随机验证码字符串
        /// </summary>
        /// <param name="len">验证码位数</param>
        /// <returns>验证码</returns>
        public static string GenerateCode(int len)
        {
            string source = "ABCDEFGHIJKLMNPQRSTUVWXYA123456789";
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            for (int i = 1; i <= len; i++)
            {
                sb.Append(source[random.Next(source.Length)]);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 将小写数字转为大写
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string ConvertChineseCurrency(decimal num)
        {
            string str1 = "零壹贰叁肆伍陆柒捌玖";            //0-9所对应的汉字 
            string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字 
            string str3 = "";    //从原num值中取出的值 
            string str4 = "";    //数字的字符串形式 
            string str5 = "";  //人民币大写金额形式 
            int i;    //循环变量 
            int j;    //num的值乘以100的字符串长度 
            string ch1 = "";    //数字的汉语读法 
            string ch2 = "";    //数字位的汉字读法 
            int nzero = 0;  //用来计算连续的零值是几个 
            int temp;            //从原num值中取出的值 

            num = Math.Round(Math.Abs(num), 2);    //将num取绝对值并四舍五入取2位小数 
            str4 = ((long)(num * 100)).ToString();        //将num乘100并转换成字符串形式 
            j = str4.Length;      //找出最高位 
            if (j > 15) { return "溢出"; }
            str2 = str2.Substring(15 - j);   //取出对应位数的str2的值。如：200.55,j为5所以str2=佰拾元角分 

            //循环取出每一位需要转换的值 
            for (i = 0; i < j; i++)
            {
                str3 = str4.Substring(i, 1);          //取出需转换的某一位的值 
                temp = Convert.ToInt32(str3);      //转换为数字 
                if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    //当所取位数不为元、万、亿、万亿上的数字时 
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if (str3 != "0" && nzero != 0)
                        {
                            ch1 = "零" + str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                    }
                }
                else
                {
                    //该位是万亿，亿，万，元位等关键位 
                    if (str3 != "0" && nzero != 0)
                    {
                        ch1 = "零" + str1.Substring(temp * 1, 1);
                        ch2 = str2.Substring(i, 1);
                        nzero = 0;
                    }
                    else
                    {
                        if (str3 != "0" && nzero == 0)
                        {
                            ch1 = str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            if (str3 == "0" && nzero >= 3)
                            {
                                ch1 = "";
                                ch2 = "";
                                nzero = nzero + 1;
                            }
                            else
                            {
                                if (j >= 11)
                                {
                                    ch1 = "";
                                    nzero = nzero + 1;
                                }
                                else
                                {
                                    ch1 = "";
                                    ch2 = str2.Substring(i, 1);
                                    nzero = nzero + 1;
                                }
                            }
                        }
                    }
                }
                if (i == (j - 11) || i == (j - 3))
                {
                    //如果该位是亿位或元位，则必须写上 
                    ch2 = str2.Substring(i, 1);
                }
                str5 = str5 + ch1 + ch2;

                if (i == j - 1 && str3 == "0")
                {
                    //最后一位（分）为0时，加上“整” 
                    str5 = str5 + '整';
                }
            }
            if (num == 0)
            {
                str5 = "零元整";
            }
            return str5;
        }

        /// <summary>
        /// 判断字符串的内容是否为整数32
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInteger32(string str)
        {
            try
            {
                int i = Convert.ToInt32(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断字符串的内容是否为整数64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInteger64(string str)
        {
            try
            {
                Int64 i = Convert.ToInt64(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断字符串的内容是否为Decimal型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDecimal(string str)
        {
            try
            {
                decimal i = Convert.ToDecimal(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断字符串的内容是否为Double型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDouble(string str)
        {
            try
            {
                double i = Convert.ToDouble(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
